// ----------------------------------
// RSDK Project: Sonic Essence
// Script Description: Chao
// Script Author: Christian Whitehead/Simon Thomley/Tails LP
// Edits by Team ReDream
// ----------------------------------

// Aliases
public value AfterImagesCheck = 0;

private alias 10 : TYPE_CHAO

private alias object.value1 : chao.targetPlayer

private alias 0 : STATE_UNATTRACTED
private alias 1 : STATE_ATTRACTED

// Player aliases
private alias object.state   : player.state
private alias object.xpos    : player.xpos
private alias object.ypos    : player.ypos
private alias object.value0  : player.Chaos
private alias object.value16 : player.isPlayer2
private alias object.value18 : player.currentPlane
private alias object.value37 : player.shield

private alias 4 : SHIELD_LIGHTNING

// Function declarations
reserve function Chao_DebugDraw
reserve function Chao_DebugSpawn

function Chao_DebugDraw
	DrawSprite(0)
end function


function Chao_DebugSpawn
	CreateTempObject(TypeName[Chao], 0, object.xpos, object.ypos)
	object[tempObjectPos].drawOrder = 4
end function


event ObjectMain
			BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == true
				// Player is touching Chao - collect it

				// Turn this object into a sparkle
				object.type = TypeName[Blank Object]

				// Set this object to the same drawOrder as the player
				object.drawOrder = player[currentPlayer].currentPlane

				// Add it to the player's Chao count

				// Play the Chao sound
				if ringPan == 0
					PlaySfx(SfxName[Chao], 0)
					SetSfxAttributes(SfxName[Chao], -1, -100)
					ringPan = 1
				else
					PlaySfx(SfxName[Chao], 0)
					SetSfxAttributes(SfxName[Chao], -1, 100)
					ringPan = 0
				end if
			end if
end event


event ObjectDraw

	// All Chaos use the same frame
	// (Animation is handled by Stage Setup object)
	DrawSprite(0)

end event


event ObjectStartup

	LoadSpriteSheet("Global/Items.gif")

	// Set each Chao's draw order to 4
	foreach (TypeName[Chao], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 4
	next

	// Chao frames
	SpriteFrame(-8, -8, 13, 20, 136, 1)

	// Add the Chao to the debug mode object list
	SetTableValue(TypeName[Chao], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Chao_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Chao_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
	
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
